package Presentacion;

import ModeloLogicoSistema.AdministradorCliente;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;

/**
 *
 * @author debo
 */
public class AltaCliente extends javax.swing.JDialog {

    AdministradorCliente ac = new AdministradorCliente();
    private boolean registroCompleto=false;

    /**
     * Creates new form AltaCliente
     */
    public AltaCliente(java.awt.Frame parent, boolean modal) throws SQLException {

        super(parent, modal);
        initComponents();
        this.setTitle("      Alta De Cliente ");
        this.setLocationRelativeTo(null);
        txtPreCel.setText("15");

        Integer id = ac.nuevoCodigoCliente();
        txtID.setText(id.toString());
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtID = new javax.swing.JTextField();
        txtApellido = new javax.swing.JTextField();
        txtPreCel = new javax.swing.JTextField();
        txtLocalidad = new javax.swing.JTextField();
        txtCalle = new javax.swing.JTextField();
        txtNumero = new javax.swing.JTextField();
        txtCP = new javax.swing.JTextField();
        jButton1 = new javax.swing.JButton();
        BtnLimpiar = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        txtCel = new javax.swing.JTextField();
        txtTel = new javax.swing.JTextField();
        jLabel10 = new javax.swing.JLabel();
        txtPiso = new javax.swing.JTextField();
        txtNombre = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        getContentPane().setLayout(new org.netbeans.lib.awtextra.AbsoluteLayout());

        jLabel1.setText("ID Cliente");
        getContentPane().add(jLabel1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 11, -1, -1));

        jLabel2.setText("Nombre");
        getContentPane().add(jLabel2, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 40, -1, -1));

        jLabel3.setText("Apellido");
        getContentPane().add(jLabel3, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 71, -1, -1));

        jLabel4.setText("Telefono");
        getContentPane().add(jLabel4, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 102, -1, -1));

        jLabel5.setText("Celular");
        getContentPane().add(jLabel5, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 128, -1, -1));

        jLabel6.setText("Localidad");
        getContentPane().add(jLabel6, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 159, -1, -1));

        jLabel7.setText("Calle");
        getContentPane().add(jLabel7, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 190, -1, -1));

        jLabel8.setText("Numero");
        getContentPane().add(jLabel8, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 221, -1, -1));

        jLabel9.setText("C.P");
        getContentPane().add(jLabel9, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 259, -1, -1));

        txtID.setEditable(false);
        txtID.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtIDActionPerformed(evt);
            }
        });
        getContentPane().add(txtID, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 10, 150, -1));

        txtApellido.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtApellidoActionPerformed(evt);
            }
        });
        getContentPane().add(txtApellido, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 70, 150, -1));

        txtPreCel.setEditable(false);
        txtPreCel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPreCelActionPerformed(evt);
            }
        });
        getContentPane().add(txtPreCel, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 130, 40, -1));
        getContentPane().add(txtLocalidad, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 160, 150, -1));
        getContentPane().add(txtCalle, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 190, 150, -1));
        getContentPane().add(txtNumero, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 220, 150, -1));

        txtCP.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtCPActionPerformed(evt);
            }
        });
        getContentPane().add(txtCP, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 250, 150, -1));

        jButton1.setText("Registrar");
        jButton1.setPreferredSize(new java.awt.Dimension(80, 23));
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 330, -1, -1));

        BtnLimpiar.setText("Limpiar");
        BtnLimpiar.setPreferredSize(new java.awt.Dimension(80, 23));
        BtnLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                BtnLimpiarActionPerformed(evt);
            }
        });
        getContentPane().add(BtnLimpiar, new org.netbeans.lib.awtextra.AbsoluteConstraints(100, 330, -1, -1));

        jButton3.setText("Cerrar");
        jButton3.setMaximumSize(new java.awt.Dimension(80, 23));
        jButton3.setPreferredSize(new java.awt.Dimension(80, 23));
        jButton3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton3ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton3, new org.netbeans.lib.awtextra.AbsoluteConstraints(190, 330, -1, -1));
        getContentPane().add(txtCel, new org.netbeans.lib.awtextra.AbsoluteConstraints(140, 130, 104, -1));

        txtTel.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtTelActionPerformed(evt);
            }
        });
        getContentPane().add(txtTel, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 100, 150, -1));

        jLabel10.setText("Piso Dpto");
        getContentPane().add(jLabel10, new org.netbeans.lib.awtextra.AbsoluteConstraints(10, 290, -1, -1));

        txtPiso.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtPisoActionPerformed(evt);
            }
        });
        getContentPane().add(txtPiso, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 280, 150, -1));

        txtNombre.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtNombreActionPerformed(evt);
            }
        });
        getContentPane().add(txtNombre, new org.netbeans.lib.awtextra.AbsoluteConstraints(95, 40, 150, -1));

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
        //Registrar
        
        String nombre = txtNombre.getText();
        String apellido = txtApellido.getText();
        if(nombre.isEmpty() | apellido.isEmpty()){
            JOptionPane.showMessageDialog(null, "Los campos nombre y apellido son obligatorios");
            return;
        }
            
        String telefono = txtTel.getText();
        String celular = txtCel.getText();
        String localidad = txtLocalidad.getText();
        String calle = txtCalle.getText();
        String numero = txtNumero.getText();
        String piso = txtPiso.getText();
        String cp = txtCP.getText();


        AdministradorCliente admCliente = null;
        try {
            admCliente = new AdministradorCliente();
        } catch (SQLException ex) {
            Logger.getLogger(AltaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        try {
            admCliente.persistirCliente(nombre, apellido, calle, numero, localidad, cp, piso, telefono, celular);
        } catch (SQLException ex) {
            Logger.getLogger(AltaCliente.class.getName()).log(Level.SEVERE, null, ex);
        }
        this.registroCompleto=true;
        this.setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed
    
    boolean estaRegistrado(){
        return registroCompleto;
    }
    
    private void BtnLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_BtnLimpiarActionPerformed
        // Cancelar
        //txtID.setText(null);
        txtApellido.setText(null);
        //txtPreCel.setText(null);
        txtLocalidad.setText(null);
        txtPiso.setText(null);
        txtNombre.setText(null);
        txtCP.setText(null);
        txtNumero.setText(null);
        txtCalle.setText(null);
        txtTel.setText(null);
        txtCel.setText(null);
    }//GEN-LAST:event_BtnLimpiarActionPerformed

    private void jButton3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton3ActionPerformed
        // Cerrar
        this.setVisible(false);
        BuscarCliente buscarCliente = new BuscarCliente(null, rootPaneCheckingEnabled);
        buscarCliente.setVisible(false);
    }//GEN-LAST:event_jButton3ActionPerformed

    private void txtIDActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtIDActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtIDActionPerformed

    private void txtApellidoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtApellidoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtApellidoActionPerformed

    private void txtTelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtTelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtTelActionPerformed

    private void txtCPActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtCPActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtCPActionPerformed

    private void txtPisoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPisoActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPisoActionPerformed

    private void txtNombreActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtNombreActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtNombreActionPerformed

    private void txtPreCelActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtPreCelActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtPreCelActionPerformed
    /**
     * @param args the command line arguments
     */
//    public static void main(String args[]) {
//        /* Set the Nimbus look and feel */
//        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
//        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
//         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
//         */
//        try {
//            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
//                if ("Nimbus".equals(info.getName())) {
//                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
//                    break;
//                }
//            }
//        } catch (ClassNotFoundException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (InstantiationException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (IllegalAccessException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
//            java.util.logging.Logger.getLogger(AltaCliente.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
//        }
//        //</editor-fold>
//
        /* Create and display the dialog */
//        java.awt.EventQueue.invokeLater(new Runnable() {
//            public void run() {
//                AltaCliente dialog = null;
//                try {
//                    dialog = new AltaCliente(new javax.swing.JFrame(), true);
//                } catch (SQLException ex) {
//                    Logger.getLogger(AltaCliente.class.getName()).log(Level.SEVERE, null, ex);
//                }
//                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
//                    @Override
//                    public void windowClosing(java.awt.event.WindowEvent e) {
//                        System.exit(0);
//                    }
//                });
//                dialog.setVisible(true);
//            }
//        });
//    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton BtnLimpiar;
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton3;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JTextField txtApellido;
    private javax.swing.JTextField txtCP;
    private javax.swing.JTextField txtCalle;
    private javax.swing.JTextField txtCel;
    private javax.swing.JTextField txtID;
    private javax.swing.JTextField txtLocalidad;
    private javax.swing.JTextField txtNombre;
    private javax.swing.JTextField txtNumero;
    private javax.swing.JTextField txtPiso;
    private javax.swing.JTextField txtPreCel;
    private javax.swing.JTextField txtTel;
    // End of variables declaration//GEN-END:variables
}
